import { TableInfo } from "@/entities/tableInfo.entity";

/**
 * 创建前端接口文件
 * 
 * @param data 
 */
const createWebApiCode = (data: TableInfo) => {
    const { fieldInfoList = [], indexInfoList = [] } = data;
    //表名下划线转小驼峰
    const smallName = data.name.replace(/_(.)/g, (a: string, b: string) => `${b.toLocaleUpperCase()}`);

    //表名下划线转大驼峰
    const bigName = data.name.replace(/(?:^|_)(\w)/g, (_, c) => c.toUpperCase());

    const str = `
    import { request } from "@umijs/max";
    export const addApi = (data: any) => {
      return request('/${smallName}/add', {
        method: 'POST',
        data: data,
      });
    }
    
    export const selectListApi = (data: any) => {
      return request('/${smallName}/selectList', {
        method: 'POST',
        data: data,
      });
    }
    
    export const deleteApi = (id: number) => {
      return request('/${smallName}/delete', {
        method: 'POST',
        data: {
          ids:[id]
        },
      });
    }
    export const updateApi = (data: any) => {
      return request('/${smallName}/update', {
        method: 'POST',
        data: data,
      });
    }
    
    export const sortApi = (data: any[]) => {
      return request('/${smallName}/sort', {
        method: 'POST',
        data: {list:data},
      });
    }
    
    export const selectOneApi = (id: any) => {
      return request('/${smallName}/selectOne', {
        method: 'POST',
        data: { id },
      });
    }
        
        
        `
    return str
}
export default createWebApiCode;